//
//  FMDownloadEngine.m
//  HuLaQuan
//
//  Created by liyan on 16/1/29.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "FMDownloadEngine.h"

@interface FMDownloadEngine()
//下载列表信息
//@property(nonatomic,strong)NSMutableDictionary *getDownloadList;
//下载url
//@property(nonatomic,strong)NSString *downloadUrl;

@end

@implementation FMDownloadEngine

-(void)getOfflineDownloadList
{
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"GetOfflineDownloadList\" User=\"%@\"><node><programmeID>2</programmeID ></node></msg>",[CurrentUser sharedInstance].ID];
    //

    NSLog(@"%@ -- src --- ",src);
    

    [YuHTTPClient XMLFMDataWithUrl:FM_SERVER_HOST parameters:src success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        NSLog(@"%@",item);
        if (isSuccess) {
            
             result = @{@"isSuccess":@YES,@"result":item};
        }
        
        else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetOfflineDownloadListFinish" object:result];
    }];
}

-(void)downloadFile:(NSString *)soundFileID
{


    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"DownloadFile\" User=\"%@\"><node><soundFileID>%@</soundFileID><attach></attach></node></msg>",[CurrentUser sharedInstance].ID,soundFileID];
    //
    NSLog(@"%@ -- src --- ",src);
    
    //use Yu

    [YuHTTPClient XMLFMDataWithUrl:FM_SERVER_HOST parameters:src success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            result = @{@"isSuccess":@YES,@"result":item};
        }
        else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadFileFinish" object:result];
        
    }];
}
-(void)downloadWithProgress:(NSString *)url progress:(void (^)(float prog))progressBlock
{
    [YuHTTPClient DownloadDataWithUrl:url parameters:nil success:^(BOOL isSuccess, id item) {
        
        NSDictionary *filePath;
        if (isSuccess) {
            filePath = @{@"isSuccess":@YES,@"result":item};
            NSLog(@"######%@",filePath);
        }
        else
        {
            filePath = @{@"isSuccess":@NO,@"message":item};
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetDownloadFilePath" object:filePath];
        
    } progress:[progressBlock copy]];
}

@end
